export default {
    data() {
        return {
            url: '',
            imageUrl: '',
            client: {}
        }
    },
    methods: {
        random_string(len) {
            len = len || 32
            var chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
            var maxPos = chars.length
            var pwd = ''
            for (let i = 0; i < len; i++) {
                pwd += chars.charAt(Math.floor(Math.random() * maxPos))
            }
            return pwd
        },
        async picUpload(file) {
            const isLt2M = file.size / 1024 / 1024 < 2;
            if (!isLt2M) {
                return this.$message.error("上传头像图片大小不能超过 2MB!");
            }
debugger
            // const res = await this.$http.get('oss/sts')
            const OSS = require('ali-oss');

            this.client = new OSS({
                // yourregion填写Bucket所在地域。以华东1（杭州）为例，Region填写为oss-cn-hangzhou。
                region: 'oss-cn-hangzhou',
                // 从STS服务获取的临时访问密钥（AccessKey ID和AccessKey Secret）。
                accessKeyId: 'LTAI5tGcay5oD7uriFuA1jKU',// res.data.AccessKeyId,
                accessKeySecret: 'UWWTm1YiOdS8ohS1kTXKzEJ63hmrST',// res.data.AccessKeySecret,
                // 填写Bucket名称。
                bucket: 'itblog',
            });
            return await this.putObject(file);
        },
        async putObject(file) {
            let result;
            const filename = this.random_string()+file.name.split('.')[1]
            try {
                result = await this.client.put(`mall/img/${filename}`, file.raw)
            } catch (e) {
                result = e
            }
            return result
        },
    }
}
